﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using IDAL;
using DataMode;

namespace DAL
{
    public class UserinfoDAL : IDAL.UserinfoIDAL
    {
        DataMode.DataModeDataContext ctx = new DataMode.DataModeDataContext();

        public IQueryable selectUser(String username)//按用户名查找用户返回
        {
            IQueryable<userinfo> userQuery = from user in ctx.userinfo where user.username.Equals(username) select user;
            return userQuery;
        }

        public void addUser(String username, String password, string email,string sex,DateTime birthday,string safe_Q,string safe_A )//往Userinfo表里增加一条新记录
        {
            userinfo user = new userinfo();
            user.username = username;
            user.password = password;
            user.email = email;
            user.sex = sex;
            user.birthday = birthday;
            user.safe_question = safe_Q;
            user.safe_answer = safe_A;
            user.user_avatar = "/img/default_avatar.jpeg";
            user.register_date = System.DateTime.Now;
            user.register_state = "未验证";
            user.state = "普通用户";
            ctx.userinfo.InsertOnSubmit(user);
            ctx.SubmitChanges();
        }

        public void modify(string username, string register_state)
        {
            var user = ctx.userinfo.SingleOrDefault<userinfo>(s => s.username == username);
            
            if (user == null)
            {
                Console.WriteLine("username错误");
                return;
            }

            user.register_state = register_state;

            //执行更新操作
            ctx.SubmitChanges();
        }

        public userinfo getuserinfo(string name)
        {
            var edit = ctx.userinfo.SingleOrDefault<userinfo>(s => s.username == name);
          

            return edit;


        }

        public void UpdateUserInfo(DataMode.userinfo ui, string name)
        {
            var edit = ctx.userinfo.SingleOrDefault<userinfo>(s => s.username == name);

            edit.username = ui.username;
            edit.birthday = ui.birthday;
            edit.cellphone = ui.cellphone;
            edit.email = ui.email;
            edit.sex = ui.sex;
            edit.password = ui.password;

            ctx.SubmitChanges();
        }
    }
}
